/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "iosdrv.h"
#include "console.h"
#include "e2p.h"
#include "debug.h"
#include "syslog.h"
#include <string.h>
#include "numberical.h"

#define E2P_BUF_SIZE 64

static enConsoleResult e2p_cmd(int argc, char **argv)
{
    enConsoleResult ret = CE_OK;

    if (argc == 5)
    {
        FD fd = open(argv[1], 0, 0);
        E2prom *e2p = (E2prom *)fd;
        int channel = (int)StringToInt(argv[3]);

        if (e2p != NULL)
        {
            if (strcmp(argv[2], "read") == 0)
            {
                char _buffer[E2P_BUF_SIZE] = {0};
                E2pDataBlock _e2p_data = {0, _buffer, 64};
                if (is_int(argv[3]) && is_int(argv[4]))
                {
                    _e2p_data.addr = atoi(argv[3]);
                    _e2p_data.size = MAX(E2P_BUF_SIZE, atoi(argv[4]));
                    int result = E2PRead(e2p, &_e2p_data);
                    KDumpPrint(_e2p_data.buffer, _e2p_data.size);
                }
                else
                {
                    ret = CE_PARAM_ERROR;
                }
            }
        }
        else
        {
            SYSLOG(LOG_ERR, "no device found!");
        }

        close(fd);
    }
    else
    {
        KPrint("e2p [name] [read] [address] [length]\r\n");
    }

    return ret;
}

BGEIN_DEF_CMD(_e2pCmd)
    CMD_DEF_ALISA(e2p, e2p_cmd, "e2p [name] [read|write] [address] [value]")
END_DEF_CMD(_adcCmd)

int E2PWrite(E2prom *e2p, E2pDataBlock *data, int commit)
{
    int ret = ATOM_OK;
    int flag = E2P_WRITE;

    if (commit)
    {
        flag = flag | E2P_COMMIT;
    }

    atomMutexGet(&e2p->Protected.lock, 0);
    ret = ioctl((FD)e2p, flag, data);
    atomMutexPut(&e2p->Protected.lock);

    return ret;
}

int E2PRead(E2prom *e2p, E2pDataBlock *data)
{
    int ret = ATOM_OK;

    atomMutexGet(&e2p->Protected.lock, 0);
    ret = ioctl((FD)e2p, E2P_READ, data);
    atomMutexPut(&e2p->Protected.lock);

    return ret;
}

void E2PCreate(E2prom *e2p, const char *name, E2pOps *ops)
{
    e2p->super.popen = NULL;
    e2p->super.pioctl = ops->e2p_ioctl;
    e2p->super.pread = NULL;
    e2p->super.pwrite = NULL;
    e2p->super.pclose = NULL;
    e2p->Protected.ops = ops;

    atomMutexCreate(&e2p->Protected.lock);

    IosDrvInstall(&e2p->super, name);

#if defined(BSP_USE_E2P) && defined(COM_USE_E2P_CMD)
    CONSOLE_REG_CMD(_e2pCmd);
#endif
}
